package com.googlecode.continuity.core.client;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * A Group is a simple implementation of the {@link com.googlecode.continuity.core.client.ContinuityGroup} interface.
 * Groups are used for various purposes such as assigning a set of rights to a set of users or to associate a set of
 * users with another object.
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class Group implements ContinuityGroup, IsSerializable, Serializable {
// ------------------------------ FIELDS ------------------------------

  /**
   * @gwt.typeArgs <com.googlecode.continuity.core.client.ContinuityUser>
   */
  private Set members;
  private String name;

  private int id;

// --------------------- GETTER / SETTER METHODS ---------------------

  /**
   * Returns this group's unique id.
   *
   * @return the group's id
   */
  public int getId() {
    return id;
  }

  /**
   * Sets this group's unique id.
   *
   * @param id the group's new id
   */
  public void setId(int id) {
    this.id = id;
  }

  /**
   * Returns the members of this group.
   *
   * @return members of this group.
   */
  public Set getMembers() {
    if (members == null) {
      members = new HashSet();
    }
    return members;
  }

  /**
   * Sets the members of this group.
   *
   * @param members new members of this group
   */
  public void setMembers(Set members) {
    this.members = members;
  }

  /**
   * Returns the name of this group.
   *
   * @return the name of this group
   */
  public String getName() {
    return name;
  }

  /**
   * Sets a new name for this group.
   *
   * @param name new group name
   */
  public void setName(String name) {
    this.name = name;
  }

// ------------------------ INTERFACE METHODS ------------------------

// --------------------- Interface ContinuityGroup ---------------------

  /**
   * Returns <code>true</code> if the user is a member of this group.
   *
   * @param user possible member
   * @return <code>true</code> when the user is a member of this group, false otherwise
   */
  public boolean isMember(ContinuityUser user) {
    return getMembers().contains(user);
  }

  /**
   * Returns the number of members in this group.
   *
   * @return number of members in this group
   */
  public int getSize() {
    return members.size();
  }

  /**
   * Adds a member to this group.
   *
   * @param member the user to be added to this group
   */
  public void addMember(ContinuityUser member) {
    getMembers().add(member);
  }
}
